package com.meiyu.live.tv.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;

import com.meiyu.live.R;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;

/**
 * Created by zbl on 2016/12/16.
 */
public class AtmosphereView extends PopAnimateView {

    private static final long ANIMATION_DURATION = 4000;
    private static final long POP_ADD_GAP = 200;
    private static final int POP_BUNDLE_COUNT = 5;

    private static final int[] pop_res = {
            R.drawable.pop_h_b,
            R.drawable.pop_s_b,
            R.drawable.pop_h_y,
            R.drawable.pop_s_y,
            R.drawable.pop_h_r,
            R.drawable.pop_s_r,
            R.drawable.pop_h_g,
            R.drawable.pop_s_g,
            R.drawable.pop_h_p,
            R.drawable.pop_s_p,
    };

    private Context context;
    PopAnimateView.Adapter<PopStar> adapter;
    private HashMap<Integer, Bitmap> imageMap = new HashMap<>();
    private Random random = new Random();
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private Handler handler = new Handler();

    private AddPopTask addPopTask;

    private class AddPopTask implements Runnable {

        private int count = POP_BUNDLE_COUNT;

        public AddPopTask(int count) {
            this.count = count;
        }

        public void add(int count) {
            this.count += count;
        }

        @Override
        public void run() {
            if (count > 0) {
                PopStar popStar = obtain(ANIMATION_DURATION);
                popStar.insert();
                count--;
                handler.postDelayed(this, POP_ADD_GAP);
            } else {
                addPopTask = null;
            }
        }
    }

    public class PopStar extends PopHolder {
        int drawableRes;
        float phase;
        float period;
    }

    public AtmosphereView(Context context) {
        super(context);
        init(context);
    }

    public AtmosphereView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AtmosphereView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context ctx) {
        this.context = ctx;
        adapter = new Adapter<PopStar>() {
            @Override
            protected void onDrawItem(PopStar popHolder, Canvas canvas, float interpolationValue) {
//                Log.e("AtmosphereView", "interpolationValue : " + interpolationValue);
                int width = canvas.getWidth();
                int height = canvas.getHeight();
                float x = width * 3 / 4 + (float) (width / 8 * interpolationValue * Math.sin(popHolder.period * Math.PI * interpolationValue - popHolder.phase));
                float y = (1f - interpolationValue) * height;
                paint.setAlpha((int) ((1f - interpolationValue) * 255));
                Bitmap bitmap = imageMap.get(popHolder.drawableRes);
                if (bitmap == null) {
                    bitmap = BitmapFactory.decodeResource(context.getResources(), popHolder.drawableRes);
                    imageMap.put(popHolder.drawableRes, bitmap);
                }
                canvas.drawBitmap(bitmap, x, y, paint);
            }

            @Override
            protected PopStar onCreatePopHolder() {
                PopStar popStar = new PopStar();
                popStar.drawableRes = pop_res[random.nextInt(10)];
                popStar.period = (float) (random.nextFloat() * 2 + 1);
                popStar.phase = (float) (random.nextFloat() * popStar.period / 2 * Math.PI);
                return popStar;
            }
        };
        setAdapter(adapter);
    }

    public void addOne() {
        if (addPopTask == null) {
            addPopTask = new AddPopTask(POP_BUNDLE_COUNT);
            handler.post(addPopTask);
        } else {
            addPopTask.add(POP_BUNDLE_COUNT);
        }
    }

    public void stopAdd() {
        if (addPopTask != null) {
            handler.removeCallbacks(addPopTask);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        onDestory();
    }

    private void onDestory() {
        Iterator<Bitmap> iterator = imageMap.values().iterator();
        while (iterator.hasNext()) {
            Bitmap bitmap = iterator.next();
            if (bitmap != null && !bitmap.isRecycled()) {
                bitmap.recycle();
            }
        }
        imageMap.clear();
    }
}
